﻿using ProgrammersCalculator.Command;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using ProgrammersCalculator.Common;

namespace ProgrammersCalculator.ViewModel
{
    public class StringViewModel : INotifyPropertyChanged
    {
        private ICommand _radioChangedCommand;
        
        private string _inputData;
        private string _outputData;

        public string OutputData
        {
            get { return _outputData; }
            set { _outputData = value;
                
                NotifyPropertyChanged("OutputData");
                
            }
        }
        public string InputData
        {
            get { return _inputData; }
            set { _inputData = value; NotifyPropertyChanged("InputData");
            Refresh();
            }
        }
        public ICommand CheckBoxChangedCommand
        {
            get { return _radioChangedCommand ?? (_radioChangedCommand = new RelayCommand(HandleRadioButton)); }
            set
            {
                _radioChangedCommand = value;
                NotifyPropertyChanged("CheckBoxChangedCommand");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string name)
        {

            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
        public string FormatType { get; set; }
        private void HandleRadioButton(object obj)
        {

            FormatType = obj.ToString();
            Refresh();
        }

        private void Refresh()
        {
            if(string.IsNullOrEmpty(FormatType))
                FormatType="Binary Format";
            if (FormatType != "Binary Format")
            {
                OutputData = Encoding.UTF8.GetBytes(InputData).ByteToHexString();
            }
            else
            {
                                OutputData = InputData.StringToBinary();
            }
        }
    }
}
